package com.zxg.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CommonUtil {
  public static Double parseDouble(String s, double defaultValue) {
    try {
      return Double.parseDouble(s);
    } catch (Exception e) {
      return defaultValue;
    }
  }

  public static String parseDate(long date, String format) {
    try {
      return new SimpleDateFormat(format).format(new Date(date));
    } catch (Exception e) {
      return "";
    }
  }
  public static Long parseLong(String s, long defaultValue) {
    try {
      return Long.parseLong(s);
    } catch (Exception e) {
      return defaultValue;
    }
  }
  public static String formate(double d){

		NumberFormat  nf = NumberFormat.getInstance();//NumberFormat是java.text.NumberFormat包里的类

		nf.setMinimumFractionDigits(2);//保留小数点两位
		nf.setMaximumFractionDigits(2);
		return nf.format(d);

	}

  public static Double parseDouble(String s) {
    return parseDouble(s, 0);
  }

  public static Long parseLong(String s) {
    return parseLong(s, 0);
  }

  public static String join(Iterator<? extends Object> it, char sep) {
    if (it == null || !it.hasNext())
      return null;
    StringBuffer sb = new StringBuffer();
    while (it.hasNext()) {
      Object o = it.next();
      sb.append(o.toString()).append(sep);
    }
    String s = sb.substring(0, sb.length() - 1);
    return s;
  }

  public static void main(String[] args) throws ParseException {
    // List<String> arrs = new ArrayList<String>();
    // arrs.add("12");
    // arrs.add("22");
    // arrs.add("32");
    // System.out.println(CommonUtil.join(arrs.iterator(),','));

  }

  public static boolean isEmpty(Collection<? extends Object> c) {
    return c == null || c.size() == 0;
  }

  public static Date parseDate(String format, String date) {
    try {
      return new SimpleDateFormat(format).parse(date);
    } catch (Exception e) {
      return null;
    }
  }

  public static String formatDate(String format, Date date) {
    return new SimpleDateFormat(format).format(date);
  }

  public static Date parseSMTDateStringToCNDate(String smtDateString) {
    if (smtDateString == null)
      return null;
    long bjtime = 8 * 3600 * 1000;
    try {
      String zone = null;
      if (smtDateString.indexOf("-") > 0) {
        zone = smtDateString.substring(smtDateString.indexOf("-") + 1, smtDateString.indexOf("-") + 3);

        Date date = new SimpleDateFormat("yyyyMMddHHmmssS").parse(smtDateString);
        if (zone == null)
          return date;
        int z = Integer.parseInt(zone);

        long time = date.getTime() + z * 3600 * 1000;
        return new Date(time + bjtime);
      }

      if (smtDateString.indexOf("+") > 0) {
        zone = smtDateString.substring(smtDateString.indexOf("+") + 1, smtDateString.indexOf("+") + 3);

        Date date = new SimpleDateFormat("yyyyMMddHHmmssS").parse(smtDateString);
        if (zone == null)
          return date;
        int z = Integer.parseInt(zone);

        long time = date.getTime() - z * 3600 * 1000;
        return new Date(time + bjtime);
      }

    } catch (Exception e) {
      return null;
    }
    return null;
  }

  public static Map<String, String> getParamsMap(String url) {
    Map<String, String> rtn = new HashMap<String, String>();
    if (url.contains("?")) {
      String pas = url.substring(url.indexOf("?") + 1);
      String ppd[] = pas.split("&");
      for (String ps : ppd) {
        if (ps != null) {
          String ss[] = ps.split("=");
          if (ss != null && ss.length == 2) {
            rtn.put(ss[0], ss[1]);
          }
        }
      }
    }
    return rtn;

  }
}
